<template>
  <div class="con">
    <!-- 放置内容区 -->
    <div class="contbox6_10" v-if="type == 1">
      <!-- 机构权益列表 -->
      <p class="titlelist6_10 titles">
        权益管理
        <span style="color: #999; font-size: 16px">
          &nbsp;&nbsp;
          <span style="font-size: 16px; color: #2f2c29">/</span>
          &nbsp;&nbsp; </span
        >机构权益
      </p>
      <div class="content">
        <div class="sousuojg6_10">
          <el-form ref="form" :model="form" label-width="100px">
            <div>
              <el-form-item label="归属OEM" style="float: left">
                <el-input
                  v-model="form.oemName"
                  placeholder="请输入归属OEM"
                ></el-input>
              </el-form-item>
              <el-form-item label="归属机构" style="float: left">
                <el-input
                  v-model="mechanism"
                  @focus="selectInstitution"
                  readonly
                  style="width: 200px"
                  placeholder="请选择归属机构"
                ></el-input>
              </el-form-item>
              <el-form-item label="机构名称" style="float: left">
                <el-input
                  v-model="form.officeName"
                  placeholder="请输入机构名称"
                ></el-input>
              </el-form-item>
              <el-form-item label="手机号" style="float: left">
                <el-input
                  v-model="form.officePhone"
                  placeholder="请输入手机号"
                ></el-input>
              </el-form-item>
            </div>
            <div class="clearfix">
              <el-form-item label="计算方式" style="float: left">
                <el-select
                  v-model="form.rgtCalcType"
                  @change="memberOffice"
                  placeholder="请选择计算方式"
                >
                  <el-option
                    style="width: 300px"
                    v-for="item in memberType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="业务品牌" style="float: left">
                <el-select
                  class="appointVip"
                  v-model="form.channelUid"
                  placeholder="请选择业务品牌"
                  @change="vipGrade()"
                >
                  <el-option
                    v-for="(val, index) in paizi"
                    :label="val.channelName"
                    :value="val.uid"
                    :key="index"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="权益等级名称" style="float: left">
                <el-input
                  v-model="form.rgtName"
                  placeholder="请输入权益等级名称"
                ></el-input>
              </el-form-item>

              <el-form-item label="业务类型" style="float: left">
                <el-select
                  v-model="form.busUid"
                  placeholder="请选择业务类型"
                >
                  <el-option
                    style="width: 300px"
                    v-for="item in typesOptions"
                    :key="item.uid"
                    :label="item.busName"
                    :value="item.uid"
                  ></el-option>
                </el-select>
              </el-form-item>


              <el-form-item label="升级时间" style="float: left">
                <el-date-picker
                  class="time"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="开始日期"
                  v-model="form.effectDateStart"
                ></el-date-picker>
                <div class="times">至</div>
                <el-date-picker
                  class="time"
                  v-model="form.effectDateEnd"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="更新时间" style="float: left">
                <el-date-picker
                  class="time"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="开始日期"
                  v-model="form.updateDateStart"
                ></el-date-picker>
                <div class="times">至</div>
                <el-date-picker
                  class="time"
                  v-model="form.updateDateEnd"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="buttons">
          <el-button
            v-if="isAuth('institutionalSearch')"
            type="primary"
            @click="onSubmit"
            >搜索</el-button
          >
          <el-button v-if="isAuth('institutionalReset')" @click="rest"
            >重置</el-button
          >
        </div>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          :header-cell-style="{ background: '#F9FAFC', color: '#222' }"
          stripe
          border
          :row-style="{ height: '0px' }"
          :cell-style="{ padding: '0px' }"
          style="width: 98%; margin-bottom: 20px; margin-left: 20px"
          @selection-change="handleSelectionChange"
        >
          
          <el-table-column
            prop="oemName"
            label="归属OEM"
            show-overflow-tooltip
            min-width="200"
          ></el-table-column>
           <el-table-column
            prop="parentOfficeName"
            label="归属机构"
            show-overflow-tooltip
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="officeName"
            label="机构名称"
            show-overflow-tooltip
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="officePhone"
            label="手机号"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="channelName"
            label="业务品牌"
            show-overflow-tooltip
            min-width="120"
          ></el-table-column>
           <el-table-column
            prop="busName"
            label="业务类型"
            show-overflow-tooltip
            min-width="120"
          ></el-table-column>
          <el-table-column
            label="权益等级"
            min-width="80"
          >
            <template slot-scope="scope">
              <div>
                V{{scope.row.level}}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="rgtName"
            show-overflow-tooltip
            label="权益等级名称"
            min-width="110"
          ></el-table-column>
          <el-table-column
            label="计算方式"
            show-overflow-tooltip
            prop="rgtCalcTypeName"
            min-width="100"
          >
            <!-- <template slot-scope="scope">
              <span>
                {{ scope.row.mbrType == "1" ? "刷脸快捷" : "交易推送" }}
              </span>
            </template> -->
          </el-table-column>
          <el-table-column
            prop="effectDate"
            label="升级时间"
            min-width="150"
          ></el-table-column>
          <el-table-column
            prop="updateDate"
            label="最近变更时间"
            min-width="150"
          ></el-table-column>
          <el-table-column label="操作" min-width="80" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button
                type="text"
                v-if="isAuth('institutionalChange')"
                @click="handleEdit(scope.row, scope.row.uid, scope.row.name)"
                >等级变更</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: right"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.page"
          :page-sizes="[20, 50, 100]"
          :page-size="form.number"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalNum"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      title="权益等级变更"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="600px"
    >
      <el-form :model="forms" label-width="120px">
        <div class="toop">提示：调整权益等级后实时生效</div>
        <el-Col style="margin-top: 10px">
          <el-form-item label="机构名称：" prop="productDesc">
            {{ forms.officeName }}
          </el-form-item>
        </el-Col>
        <el-Col>
          <el-form-item label="品牌名称：" prop="productDesc">
            {{ forms.channelName }}
          </el-form-item>
        </el-Col>
        <el-Col>
          <el-form-item label="权益类型：" prop="productDesc">
            {{ this.forms.rgtCalcTypeName }}
          </el-form-item>
        </el-Col>
        <el-Col>
          <el-form-item label="当前等级：" prop="productDesc">
            V{{ forms.level }}
          </el-form-item>
        </el-Col>
        <el-Col>
          <el-form-item label="权益等级名称：" prop="productDesc">
            {{ forms.rgtName }}
          </el-form-item>
        </el-Col>

        <el-Col>
          <el-form-item
            label="变更等级："
            style="margin-top: 10px"
            prop="productDesc"
          >
            <el-select
              style="width: 100%"
              :disabled="sign"
              v-model="forms.mbrUid"
              placeholder="请选择权益等级"
              class="filter-item"
            >
              <el-option
                v-for="item in gradeChange"
                :key="item.uid"
                :label="item.name"
                :value="item.uid"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-Col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogVisible = false;
            forms.mbrUid = '';
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
    <Tree :showType="showType" @func="transmit" />
  </div>
</template>

<script>
import Tree from "../tree/index";
import axios from "axios";
import https from "../../plugins/https.js";
// 引入比较时间大小
import { CompareDate } from "../../assets/js/commion";
export default {
  name: "route",
  data() {
    return {
      dialogVisible: false,
      mechanismAdd: false,
      gradeChange: [],
      mechanismDetails: false,
      mechanismBsiness: false,
      readonly: true,
      typebox: 0,
      treeBool: false,
      showType: false,
      type: 1,
      mechanism: "",
      currentPage: 1, //默认显示第一页
      pageSize: 10, //默认每页显示10条
      totalNum: 0, //总页数
      msg: [],
      memberType: [],
      memberTypsText: "",
      // 业务类型
      typesOptions:[],
      // value: '',
      newType: "",
      checkedAll: false,
      forms: {
        officeUid: "",
        channelUid: "",
        mbrUid: "",
        level: "",
        merName: "",
        channelName: "",
        officeName: "",
        mbrType: "",
        mbrName: "",
        rgtCalcTypeName: "", //权益类型
      },
      form: {
        busUid:"",
        rgtCalcType: "",
        channelName: "",
        parentOfficeUid: "",
        status: "",
        mbrUid: "",
        officePhone: "",
        effectDateStart: "",
        effectDateEnd: "",
        oemUid:
          localStorage.getItem("jurisdiction") != 2
            ? localStorage.getItem("uid")
            : "", //OEMid
        updateDateStart: "",
        mbrUid: "",
        updateDateEnd: "",
        mbrType: "",
        officeName: "",
        officeUid: "",
        oemName: "",
        level: "",
        page: 1,
        number: 20,
      },
      channelUid: "",
      tableData: [],
      paizi: [],
      multipleSelection: [],
      huiyuanzhazi: [],
      cunOemUid: "",
      //   urltype: this.GLOBAL.httpurl,
    };
  },
  components: {
    Tree,
  },
  created() {
    var _this = this;
    https
      .vipBrandList({ oemUid: localStorage.getItem("uid") })
      .then(function (d) {
        if (d.data.code == "0000") {
          _this.paizi = d.data.data;
          /*  _this.form.toOfficeUid = d.data.oemuid;*/
        } else {
          layer.open({
            content: d.data.msg,
            skin: "msg",
            time: 2, //1秒后自动关闭
          });
        }
      })
      .catch(function (error) {
        //axios请求失败回调
        console.log(error);
      });

    this.first();
    this.vipTypes();
    this.types()
  },
  methods: {
    // 获取业务类型
    types(){
      https.getAllBusInfo({ status: "0" }).then(res => {
        console.log(res);
        this.typesOptions = res.data.data;
      })
      .catch(err => {});
    },
    // 权益类型
    vipTypes() {
      https
        .queryDictList({ type: "calc_type" })
        .then((res) => {
          console.log("权益类型");
          console.log(res.data.data);
          this.memberType = res.data.data;
        })
        .catch((err) => {});
    },
    //这里开始
    transmit(data) {
      console.log(data.uid);
      //归属机构的uid
      this.form.officeUid = data.uid;
      console.log(this.form.officeUid);
      //归属机构名字
      this.mechanism = data.name;
      this.form.parentOfficeUid = data.uid;
    },
    //设置取消的
    determine() {
      // alert("执行")
      this.showType = false;
    },

    selectInstitution() {
      this.showType = true;
    },
    // 权益等级
    vipGrade() {
      this.form.mbrUid = "";
      let _this = this;
       axios
        .post("/api/rgt/dropDown", { channelUid:_this.form.channelUid })
        .then((res) => {
          // this.gradeChange = res.data.data;
          _this.huiyuanzhazi = res.data.data
        })
        .catch((err) => {
          console.log(err);
        });

      // https
      //   .vipGrade({ channelUid: _this.form.channelUid })
      //   .then(function (d) {
      //     if (d.data.code == "0000") {
      //       console.log(d);
      //       _this.huiyuanzhazi = d.data.data;
      //     } else {
      //       layer.open({
      //         content: d.data.msg,
      //         skin: "msg",
      //         time: 2, //22秒后自动关闭
      //       });
      //     }
      //   })
      //   .catch(function (error) {});
    },
    rest() {
      this.form.busUid = ""
      this.huiyuanzhazi = ""
      this.form.channelName = "";
      this.form.rgtName = "";
      this.form.channelUid = "";
      this.form.effectDateStart = "";
      this.form.effectDateEnd = "";
      this.form.updateDateStart = "";
      this.form.updateDateEnd = "";
      this.form.parentOfficeUid = "";
      this.form.mbrType = "";
      this.form.mbrUid = "";
      this.form.officeName = "";
      this.form.page = 1;
      this.form.oemName = "";
      this.form.officeUid = "";
      this.form.level = "";
      this.form.officePhone = "";
      (this.form.rgtCalcType = ""), (this.mechanism = "");
      this.first();
    },
    first() {
      let _this = this;
      https
        .vipOfficeMerList({
          query: _this.form,
          pageIndex: _this.form.page,
          pageSize: _this.form.number,
        })
        .then((res) => {
          console.log("获取的会员列表");
          console.log(res);
          this.tableData = res.data.data.list;
          this.totalNum = Number(res.data.data.totalSize);
        });
    },
    //   归属机构点击
    transmit(data) {
      console.log(data.uid);
      //归属机构的uid
      this.form.parentOfficeUid = data.uid;
      console.log(this.form.officeUid);
      //归属机构名字
      this.mechanism = data.name;
    },
    // 等级变更
    handleEdit(val, id, name) {
      this.forms.channelUid = val.channelUid;
      this.forms.officeUid = val.officeUid;
      this.forms.level = val.level;
      this.forms.mbrName = val.merName;
      this.forms.officeName = val.officeName;
      this.forms.channelName = val.channelName;
      this.forms.mbrType = val.mbrTypeName;
      this.forms.rgtCalcTypeName = val.rgtCalcTypeName;
      this.forms.rgtName = val.rgtName;
      this.cunOemUid = val.id;
      // https.vipQueryMember({channelUid: this.forms.channelUid}).then(res => {
      //   this.gradeChange = res.data.data;
      // }).catch(err=>{})
      axios
        .post("/api/rgt/dropDown", { oemUid: val.oemUid,channelUid:val.channelUid })
        .then((res) => {
          this.gradeChange = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
      this.forms.mbrUid = "";
      this.dialogVisible = true;
    },
    // 点击确定
    sure() {
      axios
        .post("/api/rgt/level/changeLevel", {
          rgtUid: this.forms.mbrUid,
          id: this.cunOemUid,
        })
        .then((res) => {
          console.log(res);
          this.dialogVisible = false;
          this.first();
        })
        .catch((err) => {
          console.log(err);
        });
      return;
      this.gradeChange.map((item) => {
        if (this.forms.mbrUid == item.uid) {
          this.forms.merName = item.name;
        }
      });
      if (this.forms.mbrUid == "") {
        layer.open({
          content: "请选择变更级别",
          skin: "msg",
          time: 2, //2秒后自动关闭
        });
        return false;
      }
      https
        .vipUpdateMbr(this.forms)
        .then((res) => {
          if (res.data.code == "0000") {
            layer.open({
              content: res.data.msg,
              skin: "msg",
              time: 2, //2秒后自动关闭
            });
            this.dialogVisible = false;
            this.first();
          } else {
            layer.open({
              content: res.data.msg,
              skin: "msg",
              time: 2, //2秒后自动关闭
            });
          }
        })
        .catch((err) => {});
    },
    // 搜索
    onSubmit() {
      console.log();
      if (this.form.effectDateStart != "" && this.form.effectDateEnd != "") {
        if (CompareDate(this.form.effectDateStart, this.form.effectDateEnd)) {
          this.form.page = 1;
          this.first();
        } else {
          layer.open({
            content: "结束时间不能早于开始时间",
            skin: "msg",
            time: 1, //1秒后自动关闭
          });
        }
      }else if(this.form.updateDateStart!=""&&this.form.updateDateEnd!=""){
        if (CompareDate(this.form.updateDateStart, this.form.updateDateEnd)) {
          this.form.page = 1;
          this.first();
        } else {
          layer.open({
            content: "结束时间不能早于开始时间",
            skin: "msg",
            time: 1, //1秒后自动关闭
          });
        }
      } 
      else {
        this.form.page = 1;
        this.first();
      }
    },
    // 条数改变
    handleSizeChange(val) {
      console.log(val);
      this.form.number = val;
      this.form.page = 1;
      this.first();
    },
    // 页数改变
    handleCurrentChange(val) {
      this.form.page = val;
      this.first();
    },
  },
};
</script>
<style scoped>
/deep/ .el-container[data-v-4d4a7ce2] {
  background: #f5f7fa !important;
}
.con {
  position: relative;
}
/* 单独样式 */
.contbox6_10 {
  width: 100%;
  overflow-x: auto;
  box-sizing: border-box;
  float: left;
}
.titlelist6_10 {
  position: absolute;
  padding-left: 20px;
  height: 36px;
  background: #fff;
  width: 100%;
  float: left;
  line-height: 36px;
  font-size: 16px;
  color: #222;
  border-bottom: 1px solid #ebeef5;
  border-top: 1px solid #ebeef5;
}
.content {
  float: left;
  margin-top: 200px;
  margin-bottom: 0px;
  margin: 36px 0px 0px;
  height: calc(100vh - 100px - 26px);
  background: #fff;
}
.buttons {
  width: 100%;
  float: left;
  border: none;
  margin-bottom: 10px;
}
.buttons .el-button {
  float: left;
  padding: 7px;
}
.sousuojg6_10 {
  float: left;
  box-sizing: border-box;
  font-size: 16px;
  color: #222;
  box-sizing: border-box;
  border-radius: 5px;
}
.el-form-item {
  margin-bottom: 10px;
}
.time {
  width: 136px !important;
}
.time > .el-input__inner {
  width: 100% !important;
}
.times {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.el-col-1 {
  width: 3%;
}
.nav {
  width: 100%;
  display: flex;
}
.clearfix:after {
  display: block;
  height: 0;
  clear: both;
  content: ".";
  visibility: hidden;
}
.titles {
  font-size: 16px;
}
.boxer {
  display: inline-block;
  padding-right: 20px;
  float: left;
  margin-left: 20px;
}
.boxer > .el-button {
  width: 100px;
  padding: 7px;
}
.subTtile {
  font-size: 18px;
  font-weight: bold;
}
.titlesSub {
  font-size: 18px;
  font-weight: bold;
  width: 80px;
  display: inline-block;
}
@media screen and (max-width: 1450px) {
  .el-form-item {
    width: 50% !important;
  }
}
@media screen and (max-width: 1020px) {
  .el-form-item {
    width: 100% !important;
  }
}
.exports {
  color: #409eff;
  border-color: #409eff;
}
.el-button {
  padding: 0;
}
.toop {
  width: 100%;
  background: #fff5eb;
  color: #fb932b;
  padding: 0 20px;
}
.dialog-footer > .el-button {
  padding: 12px 20px;
}
.buttons > .el-button {
  width: 100px;
  padding: 7px;
  height: 32px;
}
/deep/ .el-dialog__body {
  padding-top: 10px;
  height: 30px;
  line-height: 30px;
}
/deep/ .filter-item > .el-input {
  width: 330px;
}
/deep/ .filter-item > .el-input > .el-input__inner {
  width: 330px;
}
</style>
